﻿/*+===================================================================
File:      BossOneGun.cpp

Summary:   Định nghĩa các phương thức của CBossOneGun.
===================================================================+*/

#include "BossOneGun.h"
#include "Explosion.h"
#include "PlayState.h"

#define STATUS_FIRE    0
#define STATUS_REFIRE  1

CBossOneGun::CBossOneGun(D3DXVECTOR2 pos, int id, int typeId) : CMoveableObject(pos, id, typeId)
{
	switch (typeId)
	{
		case ID_BOSS_ONE_GUN_LEFT:
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_GUN_LEFT_REFIRE);
			m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_GUN_LEFT_FIRE);
			m_fTimeChange = 0.0f;
			m_fTimeFire = 0.4f;
			break;
		case ID_BOSS_ONE_GUN_RIGHT:
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_GUN_RIGHT_REFIRE);
			m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_GUN_RIGHT_FIRE);
			m_fTimeChange = 0.2f;
			m_fTimeFire = 0.4f;
			break;
	}
	
	m_nStatus = STATUS_REFIRE;
	m_pGun = new CGun(ID_BOSS_ONE_BULLET, 1, 1);
	m_nLife = 4;

	m_box.width = m_pSprite->GetWidth();
	m_box.height = m_pSprite->GetHeight();
}


CBossOneGun::~CBossOneGun()
{
	if (m_pGun)
		delete m_pGun;
}

void CBossOneGun::UpdateAnimation()
{
	switch (m_nStatus)
	{
		case STATUS_FIRE:
			if (m_nTypeId == ID_BOSS_ONE_GUN_LEFT)
				m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_GUN_LEFT_FIRE);
			else
				m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_GUN_RIGHT_FIRE);
			break;
		case STATUS_REFIRE:
			if (m_nTypeId == ID_BOSS_ONE_GUN_LEFT)
				m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_GUN_LEFT_REFIRE);
			else
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_GUN_RIGHT_REFIRE);
			break;
	}
}

void CBossOneGun::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	if (m_fTimeChange > 0)
	{
		m_fTimeChange -= timeFrame;
	}
	else
	{
		m_fTimeChange = m_fTimeFire;
		m_nStatus = (m_nStatus + 1) % 2;
	}

	if (m_nStatus == STATUS_FIRE)
	{
		D3DXVECTOR2 bulletPos;
		bulletPos.x = m_pos.x - m_pSprite->GetWidth() / 2.0f;
		bulletPos.y = m_pos.y;
		m_pGun->Fire(bulletPos);
	}

	UpdateBox(timeFrame);
	UpdateAnimation();
}

void CBossOneGun::UpdateCollision(vector<CGameObject*> vecGameObjs, float timeFrame)
{
	if (!m_bIsLive)
		return;

	float timeCollision = timeFrame;
	CGameObject* objCollision = NULL; // Đối tượng va chạm sớm nhất.
	float normalx = 0.0f, normaly = 0.0f;

	// Xử lí va chạm với đạn của Bill
	for (int i = 0; i < CPlayState::s_billBullets.size(); i++)
	{
		if (CPlayState::s_billBullets[i]->IsLive())
		{
			float t = CheckCollision(CPlayState::s_billBullets[i], normalx, normaly, timeFrame);

			if (t < timeFrame && t < timeCollision)
			{
				// Tìm đối tượng và thời gian va chạm sớm nhất.
				timeCollision = t;
				objCollision = CPlayState::s_billBullets[i];
			}
		}
	}

	// Xử lí va chạm
	if (objCollision != NULL)
	{
		this->Kill();
		objCollision->Kill();
	}
}

bool CBossOneGun::Kill()
{
	if (m_nLife > 1)
		m_nLife--;
	else
	{
		m_bIsLive = false;

		CExplosion* explosion = new CExplosion(m_pos, 0, ID_SMALL_EXPLOSION);
		CPlayState::s_objsOutSideQuadTree.push_back(explosion);
	}

	return true;
}